package com.kh.pajx.sop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.ISysMenuAuthDao;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.SysDictionary;
import com.kh.pajx.sop.domain.SysMenuAuth;
import com.kh.pajx.sop.service.ISysMenuAuthService;
import com.kh.pajx.sop.util.page.QueryHelper;

/**
 * 系统菜单业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月20日 下午8:44:42
 */
@Service
@Transactional
public class SysMenuAuthServiceImpl extends BaseServiceImpl<SysMenuAuth> implements ISysMenuAuthService {

	@Resource
	private ISysMenuAuthDao sysMenuAuthDao;
	
	/**
	 * 获取用于树形展示菜单的Map对象
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#getMenuTreeMap(java.util.List)
	 */
	@Override
	public Map<String,String> getMenuTreeMap(List<SysDictionary> systemDictList){
		Map<String,String> menuMap = new LinkedHashMap<String,String>();
		for (SysDictionary dict : systemDictList) {
			menuMap.put(dict.getDicName()+"-"+dict.getDicCode(),  getMenuTreeJson(dict.getDicCode()));
		}
		return menuMap;
	}
	
	/**
	 * 获取用于树形展示菜单的Map对象
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#getMenuTreeMap(java.util.List, int)
	 */
	@Override
	public Map<String,String> getMenuTreeMap(List<SysDictionary> systemDictList, int flag){
		Map<String,String> menuMap = new LinkedHashMap<String,String>();
		for (SysDictionary dict : systemDictList) {
			menuMap.put(dict.getDicName()+"-"+dict.getDicCode(),  getMenuTreeJson(dict.getDicCode(),flag));
		}
		return menuMap;
	}
	
	/**
	 * 查询所有系统顶级菜单
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findTopMenuAuthList()
	 */
	@Override
	public List<SysMenuAuth> findTopMenuAuthList() {
		return sysMenuAuthDao.findTopMenuAuthList();
	}
	
	/**
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findAllMenuListBySys(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findAllMenuListBySys(String systemCode) {
		return sysMenuAuthDao.findAllMenuListBySys(systemCode);
	}
	
	@Override
	public List<SysMenuAuth> findAllMenuListBySys(String systemCode, int flag) {
		return sysMenuAuthDao.findAllMenuListBySys(systemCode, flag);
	}
	
	/**
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findTopMenuListBySys(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findTopMenuListBySys(String systemCode) {
		return sysMenuAuthDao.findTopMenuListBySys(systemCode);
	}
	
	/**
	 * 根据系统字典编码获取该系统下所有菜单信息列表
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findMenuAuthListBySystemCode(java.lang.String)
	 
	@Override
	public List<SysMenuAuth> findMenuAuthListBySystemCode(String systemCode){
		return sysMenuAuthDao.findMenuAuthListBySystemCode(systemCode);
	}*/
	
	@Override
	public List<SysMenuAuth> getMenuTreeListBySys(String systemCode){
		List<SysMenuAuth> list = new ArrayList<SysMenuAuth>();
		walkMenuAuthTrees(findTopMenuListBySys(systemCode), "┣", list);
		return list;
	}
	
	
	// =============================================================
	
	/**
	 * 查询所有系统菜单
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#findAll()
	 */
	@Override
	public List<SysMenuAuth> findAll() {
		List<SysMenuAuth> list = new ArrayList<SysMenuAuth>();
		walkMenuAuthTrees(findTopMenuAuthList(), "┣", list);
		return list;
	}
	
	/**
	 * 分页获取系统菜单列表
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#getPageBean(int, int, com.kh.pajx.sop.util.page.QueryHelper)
	 */
	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return sysMenuAuthDao.getPageBean(pageNum, pageSize, queryHelper);
	}
	
	// =============================================================
	
	/**
	 * 根据系统编码组织该系统下用于树形展示菜单的JSON字符串
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#getMenuTreeJson(java.lang.String)
	 */
	@Override
	public String getMenuTreeJson(String systemCode){
		List<SysMenuAuth> menuList = findAllMenuListBySys(systemCode);
		String jsonStr = "";
		for (int i = 0; i < menuList.size(); i++) {
			if(i > 0){
				jsonStr += ",";
			}
			jsonStr += "{'treeId':'"+menuList.get(i).getMenuId()+"'"
					+",'pId':'"+(menuList.get(i).getParent() == null?"":menuList.get(i).getParent().getMenuId())+"'"
					+",'treeName':'"+menuList.get(i).getMenuName()+"'}";
		}
		return "[" + jsonStr + "]";
	}
	
	@Override
	public String getMenuTreeJson(String systemCode,int flag){
		List<SysMenuAuth> menuList = findAllMenuListBySys(systemCode,flag);
		String jsonStr = "";
		for (int i = 0; i < menuList.size(); i++) {
			if(i > 0){
				jsonStr += ",";
			}
			jsonStr += "{'treeId':'"+menuList.get(i).getMenuId()+"'"
					+",'pId':'"+(menuList.get(i).getParent() == null?"":menuList.get(i).getParent().getMenuId())+"'"
					+",'treeName':'"+menuList.get(i).getMenuName()+"'}";
		}
		return "[" + jsonStr + "]";
	}
	
	/**
	 * 遍历菜单树，把遍历出来的菜单放到指定的集合中
	 * @param topMenuAuthList
	 * @param prefix
	 * @param list   
	 * @author:	azzcsimp
	 * @Createdate:	2014年8月6日 下午2:16:14
	 */
	private void walkMenuAuthTrees(Collection<SysMenuAuth> topMenuAuthList, String prefix, List<SysMenuAuth> list){
		for(SysMenuAuth top : topMenuAuthList){
			SysMenuAuth copy = new SysMenuAuth();
			BeanUtils.copyProperties(top, copy);
			copy.setMenuName(prefix + copy.getMenuName());
			list.add(copy);
			walkMenuAuthTrees(top.getChildren(), "　" + prefix, list);
		}
	}
	
	
	// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	/**
	 * 添加系统菜单
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#save(java.lang.Object)
	 */
	@Override
	public void save(SysMenuAuth demo) {
		sysMenuAuthDao.save(demo);
	}
	
	/**
	 * 删除系统菜单
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#delete(java.lang.String)
	 */
	@Override
	public void delete(String uuid) {
		sysMenuAuthDao.delete(uuid);
	}
	
	/**
	 * 修改系统菜单
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#update(java.lang.Object)
	 */
	@Override
	public void update(SysMenuAuth demo) {
		sysMenuAuthDao.update(demo);
	}
	
	/**
	 * 加载系统菜单
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#getById(java.lang.String)
	 */
	@Override
	public SysMenuAuth getById(String uuid) {
		return sysMenuAuthDao.getById(uuid);
	}
	
	
	
	
	
	
	
	/**
	 * 查找二级菜单
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findChilerenMenuAuthList(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findChilerenMenuAuthList(String parentId) {
		return sysMenuAuthDao.findChilerenMenuAuthList(parentId);
	}
	
	/**
	 * 
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#counMenuAuthByPId(java.lang.String)
	 */
	@Override
	public long counMenuAuthByPId(String pId) {
		return sysMenuAuthDao.counMenuAuthByPId(pId);
	}
	
	/**
	 * 获取系统下顶级菜单
	 * @see com.kh.pajx.sop.service.ISysMenuAuthService#findTopMenuAuthListBySystemCode(java.lang.String)
	 
	@Override
	public List<SysMenuAuth> findTopMenuAuthListBySystemCode(String systemCode){
		return sysMenuAuthDao.findTopMenuAuthListBySystemCode(systemCode);
	}*/
	
	
	
	
	@Override
	public List<String> getAllMenuAuthUrls() {
		return sysMenuAuthDao.getAllMenuAuthUrls();
	}
	
	@Override
	public List<SysMenuAuth> getByIds(String[] uuids) {
		return sysMenuAuthDao.getByIds(uuids);
	}
	
	@Override
	public List<String> getAllMenuAuthUrlsBySystemCode(String systemCode) {
		return sysMenuAuthDao.getAllMenuAuthUrlsBySystemCode(systemCode);
	}
	
	@Override
	public List<SysMenuAuth> findSecondMenuAuthListBySystemCode(String systemCode) {
		return sysMenuAuthDao.findSecondMenuAuthListBySystemCode(systemCode);
	}
}
